package com.gorkr.dp;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.junit.Test;

/**
 * @author 张心宇 <zhangxinyu20@kuaishou.com>
 * Created on 2022-11-03
 */
public class M139WordBreak {
    public boolean wordBreak(String s, List<String> wordDict) {
        HashSet<String> wordSet = new HashSet<String>(wordDict);
        boolean[] dp = new boolean[s.length()];
        // base case
        dp[0] = true;
        for (int i = 0; i < dp.length; i++) {
            for (int j = 0; j < i; j++){
                if(dp[j]&&wordSet.contains(s.substring(j,i+1))){
                    dp[i] = true;
                }
            }
        }
        return dp[s.length()-1];
    }

    @Test
    void test(){
        ArrayList<String> wordDict = new ArrayList<String>();
        wordDict.add("leet");
        wordDict.add("code");
        System.out.println(wordBreak("leetcode", wordDict));
    }
}
